From 2f01868a94f85457e02465cfb53b078d4e25b4d5 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 3 Mar 2016 22:52:45 +0000 Subject: [PATCH] Add rustflags support to config files `build.rustflags` is treated exactly like `RUSTFLAGS`. It is a list, so argument lists with spaces work. `RUSTFLAGS` takes precedent, then `build.rustflags`. --- Cargo.lock | 108 +-- src/cargo/ops/cargo_compile.rs | 2 +- src/cargo/ops/cargo_rustc/context.rs | 38 +- src/cargo/ops/cargo_rustc/fingerprint.rs | 2 +- src/cargo/ops/cargo_rustc/mod.rs | 2 +- tests/test_cargo_compile.rs | 378 ----------- tests/test_cargo_compile_rustflags.rs | 826 +++++++++++++++++++++++ tests/tests.rs | 1 + 8 files changed, 906 insertions(+), 451 deletions(-) create mode 100644 tests/test_cargo_compile_rustflags.rs diff --git a/Cargo.lock b/Cargo.lock index 766d10984..e8ff0776e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,23 +13,23 @@ dependencies = [ "flate2 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", "git2 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "git2-curl 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "glob 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", + "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "hamcrest 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "libgit2-sys 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "libgit2-sys 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 0.1.55 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.58 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "tar 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", - "toml 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)", "url 0.2.38 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -37,7 +37,7 @@ name = "advapi32-sys" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -61,7 +61,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "cmake" -version = "0.1.13" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", @@ -87,10 +87,10 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "curl-sys 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", - "url 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "url 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -99,10 +99,10 @@ version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "libz-sys 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -110,7 +110,7 @@ name = "docopt" version = "0.6.78" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "regex 0.1.55 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.58 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "strsim 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -121,7 +121,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 0.1.55 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.58 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -129,7 +129,7 @@ name = "filetime" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -137,7 +137,7 @@ name = "flate2" version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "miniz-sys 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -151,7 +151,7 @@ name = "gdi32-sys" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -160,9 +160,9 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "libgit2-sys 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "url 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "libgit2-sys 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "url 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -173,12 +173,12 @@ dependencies = [ "curl 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", "git2 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", - "url 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "url 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "glob" -version = "0.2.10" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -194,27 +194,27 @@ name = "kernel32-sys" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "libc" -version = "0.2.7" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "libgit2-sys" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cmake 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", + "cmake 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "libssh2-sys 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)", "libz-sys 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -230,11 +230,11 @@ name = "libssh2-sys" version = "0.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "cmake 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "cmake 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "libz-sys 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -243,8 +243,8 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -252,7 +252,7 @@ name = "log" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -265,7 +265,7 @@ name = "memchr" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -274,12 +274,12 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "gcc 0.3.25 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "nom" -version = "1.2.1" +version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -296,7 +296,7 @@ name = "num_cpus" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -305,15 +305,15 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "gdi32-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "libressl-pnacl-sys 2.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "pkg-config 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "user32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "pkg-config" -version = "0.3.7" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -329,23 +329,23 @@ name = "rand" version = "0.3.14" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "regex" -version = "0.1.55" +version = "0.1.58" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "aho-corasick 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "regex-syntax 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "regex-syntax" -version = "0.2.5" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -358,7 +358,7 @@ name = "semver" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "nom 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "nom 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -372,7 +372,7 @@ version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "filetime 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -389,7 +389,7 @@ version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -398,13 +398,13 @@ version = "0.1.34" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "toml" -version = "0.1.27" +version = "0.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", @@ -435,7 +435,7 @@ dependencies = [ [[package]] name = "url" -version = "0.5.5" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -450,7 +450,7 @@ name = "user32-sys" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -470,7 +470,7 @@ dependencies = [ [[package]] name = "winapi" -version = "0.2.5" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index ad82d4f77..fa0ff0581 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -469,7 +469,7 @@ fn scrape_target_config(config: &Config, triple: &str) None => return Ok(ret), }; for (lib_name, _) in table.into_iter() { - if lib_name == "ar" || lib_name == "linker" { + if lib_name == "ar" || lib_name == "linker" || lib_name == "rustflags" { continue } diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 0143b3904..b40074564 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -47,7 +47,6 @@ pub struct Context<'a, 'cfg: 'a> { target_info: TargetInfo, host_info: TargetInfo, profiles: &'a Profiles, - rustflags: Option, } #[derive(Clone)] @@ -80,7 +79,6 @@ impl<'a, 'cfg> Context<'a, 'cfg> { let engine = build_config.exec_engine.as_ref().cloned().unwrap_or({ Arc::new(Box::new(ProcessEngine)) }); - let rustflags = env::var("RUSTFLAGS").ok(); Ok(Context { target_triple: target_triple, host: host, @@ -100,7 +98,6 @@ impl<'a, 'cfg> Context<'a, 'cfg> { build_scripts: HashMap::new(), build_explicit_deps: HashMap::new(), links: Links::new(), - rustflags: rustflags, }) } @@ -621,7 +618,9 @@ impl<'a, 'cfg> Context<'a, 'cfg> { &self.profiles.dev } - pub fn rustflags_args(&self, unit: &Unit) -> Vec { + // Acquire extra flags to pass to the compiler from the + // RUSTFLAGS environment variable and similar config values + pub fn rustflags_args(&self, unit: &Unit) -> CargoResult> { // We *want* to apply RUSTFLAGS only to builds for the // requested target architecture, and not to things like build // scripts and plugins, which may be for an entirely different @@ -643,22 +642,29 @@ impl<'a, 'cfg> Context<'a, 'cfg> { // RUSTFLAGS. let compiling_with_target = self.build_config.requested_target.is_some(); let is_target_kind = unit.kind == Kind::Target; - let use_rustflags = match (compiling_with_target, is_target_kind) { - (false, _) => true, - (true, true) => true, - (true, false) => false, - }; - if !use_rustflags { return Vec::new(); } + if compiling_with_target && ! is_target_kind { + // This is probably a build script or plugin and we're + // compiling with --target. In this scenario there are + // no rustflags we can apply. + return Ok(Vec::new()); + } - let mut args = Vec::new(); + // First try RUSTFLAGS from the environment + if let Some(a) = env::var("RUSTFLAGS").ok() { + let args = a.split(" ") + .map(str::trim) + .filter(|s| !s.is_empty()) + .map(str::to_string); + return Ok(args.collect()); + } - if let Some(ref a) = self.rustflags { - for s in a.split(" ") { - args.push(s.to_owned()); - } + // Then the build.rustflags value + if let Some(args) = try!(self.config.get_list("build.rustflags")) { + let args = args.val.into_iter().map(|a| a.0); + return Ok(args.collect()); } - args + Ok(Vec::new()) } } diff --git a/src/cargo/ops/cargo_rustc/fingerprint.rs b/src/cargo/ops/cargo_rustc/fingerprint.rs index 8195ee378..042e223b3 100644 --- a/src/cargo/ops/cargo_rustc/fingerprint.rs +++ b/src/cargo/ops/cargo_rustc/fingerprint.rs @@ -354,7 +354,7 @@ fn calculate<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) deps: deps, local: local, memoized_hash: Mutex::new(None), - rustflags: cx.rustflags_args(unit), + rustflags: try!(cx.rustflags_args(unit)), }); cx.fingerprints.insert(*unit, fingerprint.clone()); Ok(fingerprint) diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 41bf139b0..250870f41 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -243,7 +243,7 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult { let dep_info_loc = fingerprint::dep_info_loc(cx, unit); let cwd = cx.config.cwd().to_path_buf(); - let rustflags = cx.rustflags_args(unit); + let rustflags = try!(cx.rustflags_args(unit)); return Ok(Work::new(move |desc_tx| { // Only at runtime have we discovered what the extra -L and -l diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index 9530e9417..731546e63 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -2068,381 +2068,3 @@ test!(manifest_with_bom_is_ok { assert_that(p.cargo_process("build").arg("-v"), execs().with_status(0)); }); - -test!(rustflags_normal_source { - let p = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - "#) - .file("src/lib.rs", "") - .file("src/bin/a.rs", "fn main() {}") - .file("examples/b.rs", "fn main() {}") - .file("tests/c.rs", "#[test] fn f() { }") - .file("benches/d.rs", r#" - #![feature(test)] - extern crate test; - #[bench] fn run1(_ben: &mut test::Bencher) { }"#); - p.build(); - - // Use RUSTFLAGS to pass an argument that will generate an error - assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") - .arg("--lib"), - execs().with_status(101)); - assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") - .arg("--bin=a"), - execs().with_status(101)); - assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") - .arg("--bin=b"), - execs().with_status(101)); - assert_that(p.cargo("check").env("RUSTFLAGS", "-Z bogus"), - execs().with_status(101)); - assert_that(p.cargo("bench").env("RUSTFLAGS", "-Z bogus"), - execs().with_status(101)); -}); - -test!(rustflags_build_script { - // RUSTFLAGS should be passed to rustc for build scripts - // when --target is not specified. - // In this test if --cfg foo is passed the build will fail. - let p = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - build = "build.rs" - "#) - .file("src/lib.rs", "") - .file("build.rs", r#" - fn main() { } - #[cfg(not(foo))] - fn main() { } - "#); - p.build(); - - assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), - execs().with_status(0)); -}); - -test!(rustflags_build_script_dep { - // RUSTFLAGS should be passed to rustc for build scripts - // when --target is not specified. - // In this test if --cfg foo is not passed the build will fail. - let foo = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - build = "build.rs" - - [build-dependencies.bar] - path = "../bar" - "#) - .file("src/lib.rs", "") - .file("build.rs", r#" - fn main() { } - "#); - let bar = project("bar") - .file("Cargo.toml", r#" - [package] - name = "bar" - version = "0.0.1" - "#) - .file("src/lib.rs", r#" - fn bar() { } - #[cfg(not(foo))] - fn bar() { } - "#); - foo.build(); - bar.build(); - - assert_that(foo.cargo("build").env("RUSTFLAGS", "--cfg foo"), - execs().with_status(0)); -}); - -test!(rustflags_plugin { - // RUSTFLAGS should be passed to rustc for plugins - // when --target is not specified. - // In this test if --cfg foo is not passed the build will fail. - let p = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - - [lib] - name = "foo" - plugin = true - "#) - .file("src/lib.rs", r#" - fn main() { } - #[cfg(not(foo))] - fn main() { } - "#); - p.build(); - - assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), - execs().with_status(0)); -}); - -test!(rustflags_plugin_dep { - // RUSTFLAGS should be passed to rustc for plugins - // when --target is not specified. - // In this test if --cfg foo is not passed the build will fail. - let foo = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - - [lib] - name = "foo" - plugin = true - - [dependencies.bar] - path = "../bar" - "#) - .file("src/lib.rs", r#" - fn foo() { } - "#); - let bar = project("bar") - .file("Cargo.toml", r#" - [package] - name = "bar" - version = "0.0.1" - - [lib] - name = "bar" - "#) - .file("src/lib.rs", r#" - fn bar() { } - #[cfg(not(foo))] - fn bar() { } - "#); - foo.build(); - bar.build(); - - assert_that(foo.cargo("build").env("RUSTFLAGS", "--cfg foo"), - execs().with_status(0)); -}); - -test!(rustflags_normal_source_with_target { - let p = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - "#) - .file("src/lib.rs", "") - .file("src/bin/a.rs", "fn main() {}") - .file("examples/b.rs", "fn main() {}") - .file("tests/c.rs", "#[test] fn f() { }") - .file("benches/d.rs", r#" - #![feature(test)] - extern crate test; - #[bench] fn run1(_ben: &mut test::Bencher) { }"#); - p.build(); - - let ref host = ::rustc_host(); - - // Use RUSTFLAGS to pass an argument that will generate an error - assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") - .arg("--lib").arg("--target").arg(host), - execs().with_status(101)); - assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") - .arg("--bin=a").arg("--target").arg(host), - execs().with_status(101)); - assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") - .arg("--bin=b").arg("--target").arg(host), - execs().with_status(101)); - assert_that(p.cargo("check").env("RUSTFLAGS", "-Z bogus") - .arg("--target").arg(host), - execs().with_status(101)); - assert_that(p.cargo("bench").env("RUSTFLAGS", "-Z bogus") - .arg("--target").arg(host), - execs().with_status(101)); -}); - -test!(rustflags_build_script_with_target { - // RUSTFLAGS should not be passed to rustc for build scripts - // when --target is specified. - // In this test if --cfg foo is passed the build will fail. - let p = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - build = "build.rs" - "#) - .file("src/lib.rs", "") - .file("build.rs", r#" - fn main() { } - #[cfg(foo)] - fn main() { } - "#); - p.build(); - - let host = ::rustc_host(); - assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo") - .arg("--target").arg(host), - execs().with_status(0)); -}); - -test!(rustflags_build_script_dep_with_target { - // RUSTFLAGS should not be passed to rustc for build scripts - // when --target is specified. - // In this test if --cfg foo is passed the build will fail. - let foo = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - build = "build.rs" - - [build-dependencies.bar] - path = "../bar" - "#) - .file("src/lib.rs", "") - .file("build.rs", r#" - fn main() { } - "#); - let bar = project("bar") - .file("Cargo.toml", r#" - [package] - name = "bar" - version = "0.0.1" - "#) - .file("src/lib.rs", r#" - fn bar() { } - #[cfg(foo)] - fn bar() { } - "#); - foo.build(); - bar.build(); - - let host = ::rustc_host(); - assert_that(foo.cargo("build").env("RUSTFLAGS", "--cfg foo") - .arg("--target").arg(host), - execs().with_status(0)); -}); - -test!(rustflags_plugin_with_target { - // RUSTFLAGS should not be passed to rustc for plugins - // when --target is specified. - // In this test if --cfg foo is passed the build will fail. - let p = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - - [lib] - name = "foo" - plugin = true - "#) - .file("src/lib.rs", r#" - fn main() { } - #[cfg(foo)] - fn main() { } - "#); - p.build(); - - let host = ::rustc_host(); - assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo") - .arg("--target").arg(host), - execs().with_status(0)); -}); - -test!(rustflags_plugin_dep_with_target { - // RUSTFLAGS should not be passed to rustc for plugins - // when --target is specified. - // In this test if --cfg foo is passed the build will fail. - let foo = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - - [lib] - name = "foo" - plugin = true - - [dependencies.bar] - path = "../bar" - "#) - .file("src/lib.rs", r#" - fn foo() { } - "#); - let bar = project("bar") - .file("Cargo.toml", r#" - [package] - name = "bar" - version = "0.0.1" - - [lib] - name = "bar" - "#) - .file("src/lib.rs", r#" - fn bar() { } - #[cfg(foo)] - fn bar() { } - "#); - foo.build(); - bar.build(); - - let host = ::rustc_host(); - assert_that(foo.cargo("build").env("RUSTFLAGS", "--cfg foo") - .arg("--target").arg(host), - execs().with_status(0)); -}); - -test!(rustflags_recompile { - let p = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - "#) - .file("src/lib.rs", ""); - p.build(); - - assert_that(p.cargo("build"), - execs().with_status(0)); - // Setting RUSTFLAGS forces a recompile - assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus"), - execs().with_status(101)); -}); - -test!(rustflags_recompile2 { - let p = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - "#) - .file("src/lib.rs", ""); - p.build(); - - assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), - execs().with_status(0)); - // Setting RUSTFLAGS forces a recompile - assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus"), - execs().with_status(101)); -}); - -test!(rustflags_no_recompile { - let p = project("foo") - .file("Cargo.toml", r#" - [package] - name = "foo" - version = "0.0.1" - "#) - .file("src/lib.rs", ""); - p.build(); - - assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), - execs().with_status(0)); - assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), - execs().with_stdout("").with_status(0)); -}); diff --git a/tests/test_cargo_compile_rustflags.rs b/tests/test_cargo_compile_rustflags.rs new file mode 100644 index 000000000..a95184cad --- /dev/null +++ b/tests/test_cargo_compile_rustflags.rs @@ -0,0 +1,826 @@ +use std::io::Write; +use std::fs::{self, File}; +use support::{project, execs, paths}; +use hamcrest::assert_that; + +fn setup() { +} + +test!(env_rustflags_normal_source { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + "#) + .file("src/lib.rs", "") + .file("src/bin/a.rs", "fn main() {}") + .file("examples/b.rs", "fn main() {}") + .file("tests/c.rs", "#[test] fn f() { }") + .file("benches/d.rs", r#" + #![feature(test)] + extern crate test; + #[bench] fn run1(_ben: &mut test::Bencher) { }"#); + p.build(); + + // Use RUSTFLAGS to pass an argument that will generate an error + assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") + .arg("--lib"), + execs().with_status(101)); + assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") + .arg("--bin=a"), + execs().with_status(101)); + assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") + .arg("--example=b"), + execs().with_status(101)); + assert_that(p.cargo("test").env("RUSTFLAGS", "-Z bogus"), + execs().with_status(101)); + assert_that(p.cargo("bench").env("RUSTFLAGS", "-Z bogus"), + execs().with_status(101)); +}); + +test!(env_rustflags_build_script { + // RUSTFLAGS should be passed to rustc for build scripts + // when --target is not specified. + // In this test if --cfg foo is passed the build will fail. + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + build = "build.rs" + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + fn main() { } + #[cfg(not(foo))] + fn main() { } + "#); + p.build(); + + assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), + execs().with_status(0)); +}); + +test!(env_rustflags_build_script_dep { + // RUSTFLAGS should be passed to rustc for build scripts + // when --target is not specified. + // In this test if --cfg foo is not passed the build will fail. + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + build = "build.rs" + + [build-dependencies.bar] + path = "../bar" + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + fn main() { } + "#); + let bar = project("bar") + .file("Cargo.toml", r#" + [package] + name = "bar" + version = "0.0.1" + "#) + .file("src/lib.rs", r#" + fn bar() { } + #[cfg(not(foo))] + fn bar() { } + "#); + foo.build(); + bar.build(); + + assert_that(foo.cargo("build").env("RUSTFLAGS", "--cfg foo"), + execs().with_status(0)); +}); + +test!(env_rustflags_plugin { + // RUSTFLAGS should be passed to rustc for plugins + // when --target is not specified. + // In this test if --cfg foo is not passed the build will fail. + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + + [lib] + name = "foo" + plugin = true + "#) + .file("src/lib.rs", r#" + fn main() { } + #[cfg(not(foo))] + fn main() { } + "#); + p.build(); + + assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), + execs().with_status(0)); +}); + +test!(env_rustflags_plugin_dep { + // RUSTFLAGS should be passed to rustc for plugins + // when --target is not specified. + // In this test if --cfg foo is not passed the build will fail. + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + + [lib] + name = "foo" + plugin = true + + [dependencies.bar] + path = "../bar" + "#) + .file("src/lib.rs", r#" + fn foo() { } + "#); + let bar = project("bar") + .file("Cargo.toml", r#" + [package] + name = "bar" + version = "0.0.1" + + [lib] + name = "bar" + "#) + .file("src/lib.rs", r#" + fn bar() { } + #[cfg(not(foo))] + fn bar() { } + "#); + foo.build(); + bar.build(); + + assert_that(foo.cargo("build").env("RUSTFLAGS", "--cfg foo"), + execs().with_status(0)); +}); + +test!(env_rustflags_normal_source_with_target { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + "#) + .file("src/lib.rs", "") + .file("src/bin/a.rs", "fn main() {}") + .file("examples/b.rs", "fn main() {}") + .file("tests/c.rs", "#[test] fn f() { }") + .file("benches/d.rs", r#" + #![feature(test)] + extern crate test; + #[bench] fn run1(_ben: &mut test::Bencher) { }"#); + p.build(); + + let ref host = ::rustc_host(); + + // Use RUSTFLAGS to pass an argument that will generate an error + assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") + .arg("--lib").arg("--target").arg(host), + execs().with_status(101)); + assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") + .arg("--bin=a").arg("--target").arg(host), + execs().with_status(101)); + assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus") + .arg("--example=b").arg("--target").arg(host), + execs().with_status(101)); + assert_that(p.cargo("test").env("RUSTFLAGS", "-Z bogus") + .arg("--target").arg(host), + execs().with_status(101)); + assert_that(p.cargo("bench").env("RUSTFLAGS", "-Z bogus") + .arg("--target").arg(host), + execs().with_status(101)); +}); + +test!(env_rustflags_build_script_with_target { + // RUSTFLAGS should not be passed to rustc for build scripts + // when --target is specified. + // In this test if --cfg foo is passed the build will fail. + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + build = "build.rs" + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + fn main() { } + #[cfg(foo)] + fn main() { } + "#); + p.build(); + + let host = ::rustc_host(); + assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo") + .arg("--target").arg(host), + execs().with_status(0)); +}); + +test!(env_rustflags_build_script_dep_with_target { + // RUSTFLAGS should not be passed to rustc for build scripts + // when --target is specified. + // In this test if --cfg foo is passed the build will fail. + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + build = "build.rs" + + [build-dependencies.bar] + path = "../bar" + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + fn main() { } + "#); + let bar = project("bar") + .file("Cargo.toml", r#" + [package] + name = "bar" + version = "0.0.1" + "#) + .file("src/lib.rs", r#" + fn bar() { } + #[cfg(foo)] + fn bar() { } + "#); + foo.build(); + bar.build(); + + let host = ::rustc_host(); + assert_that(foo.cargo("build").env("RUSTFLAGS", "--cfg foo") + .arg("--target").arg(host), + execs().with_status(0)); +}); + +test!(env_rustflags_plugin_with_target { + // RUSTFLAGS should not be passed to rustc for plugins + // when --target is specified. + // In this test if --cfg foo is passed the build will fail. + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + + [lib] + name = "foo" + plugin = true + "#) + .file("src/lib.rs", r#" + fn main() { } + #[cfg(foo)] + fn main() { } + "#); + p.build(); + + let host = ::rustc_host(); + assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo") + .arg("--target").arg(host), + execs().with_status(0)); +}); + +test!(env_rustflags_plugin_dep_with_target { + // RUSTFLAGS should not be passed to rustc for plugins + // when --target is specified. + // In this test if --cfg foo is passed the build will fail. + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + + [lib] + name = "foo" + plugin = true + + [dependencies.bar] + path = "../bar" + "#) + .file("src/lib.rs", r#" + fn foo() { } + "#); + let bar = project("bar") + .file("Cargo.toml", r#" + [package] + name = "bar" + version = "0.0.1" + + [lib] + name = "bar" + "#) + .file("src/lib.rs", r#" + fn bar() { } + #[cfg(foo)] + fn bar() { } + "#); + foo.build(); + bar.build(); + + let host = ::rustc_host(); + assert_that(foo.cargo("build").env("RUSTFLAGS", "--cfg foo") + .arg("--target").arg(host), + execs().with_status(0)); +}); + +test!(env_rustflags_recompile { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + "#) + .file("src/lib.rs", ""); + p.build(); + + assert_that(p.cargo("build"), + execs().with_status(0)); + // Setting RUSTFLAGS forces a recompile + assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus"), + execs().with_status(101)); +}); + +test!(env_rustflags_recompile2 { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + "#) + .file("src/lib.rs", ""); + p.build(); + + assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), + execs().with_status(0)); + // Setting RUSTFLAGS forces a recompile + assert_that(p.cargo("build").env("RUSTFLAGS", "-Z bogus"), + execs().with_status(101)); +}); + +test!(env_rustflags_no_recompile { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + "#) + .file("src/lib.rs", ""); + p.build(); + + assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), + execs().with_status(0)); + assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), + execs().with_stdout("").with_status(0)); +}); + +test!(build_rustflags_normal_source { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + "#) + .file("src/lib.rs", "") + .file("src/bin/a.rs", "fn main() {}") + .file("examples/b.rs", "fn main() {}") + .file("tests/c.rs", "#[test] fn f() { }") + .file("benches/d.rs", r#" + #![feature(test)] + extern crate test; + #[bench] fn run1(_ben: &mut test::Bencher) { }"#) + .file(".cargo/config", r#" + [build] + rustflags = ["-Z", "bogus"] + "#); + p.build(); + + assert_that(p.cargo("build") + .arg("--lib"), + execs().with_status(101)); + assert_that(p.cargo("build") + .arg("--bin=a"), + execs().with_status(101)); + assert_that(p.cargo("build") + .arg("--example=b"), + execs().with_status(101)); + assert_that(p.cargo("test"), + execs().with_status(101)); + assert_that(p.cargo("bench"), + execs().with_status(101)); +}); + +test!(build_rustflags_build_script { + // RUSTFLAGS should be passed to rustc for build scripts + // when --target is not specified. + // In this test if --cfg foo is passed the build will fail. + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + build = "build.rs" + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + fn main() { } + #[cfg(not(foo))] + fn main() { } + "#) + .file(".cargo/config", r#" + [build] + rustflags = ["--cfg", "foo"] + "#); + p.build(); + + assert_that(p.cargo("build"), + execs().with_status(0)); +}); + +test!(build_rustflags_build_script_dep { + // RUSTFLAGS should be passed to rustc for build scripts + // when --target is not specified. + // In this test if --cfg foo is not passed the build will fail. + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + build = "build.rs" + + [build-dependencies.bar] + path = "../bar" + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + fn main() { } + "#) + .file(".cargo/config", r#" + [build] + rustflags = ["--cfg", "foo"] + "#); + let bar = project("bar") + .file("Cargo.toml", r#" + [package] + name = "bar" + version = "0.0.1" + "#) + .file("src/lib.rs", r#" + fn bar() { } + #[cfg(not(foo))] + fn bar() { } + "#); + foo.build(); + bar.build(); + + assert_that(foo.cargo("build"), + execs().with_status(0)); +}); + +test!(build_rustflags_plugin { + // RUSTFLAGS should be passed to rustc for plugins + // when --target is not specified. + // In this test if --cfg foo is not passed the build will fail. + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + + [lib] + name = "foo" + plugin = true + "#) + .file("src/lib.rs", r#" + fn main() { } + #[cfg(not(foo))] + fn main() { } + "#) + .file(".cargo/config", r#" + [build] + rustflags = ["--cfg", "foo"] + "#); + p.build(); + + assert_that(p.cargo("build"), + execs().with_status(0)); +}); + +test!(build_rustflags_plugin_dep { + // RUSTFLAGS should be passed to rustc for plugins + // when --target is not specified. + // In this test if --cfg foo is not passed the build will fail. + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + + [lib] + name = "foo" + plugin = true + + [dependencies.bar] + path = "../bar" + "#) + .file("src/lib.rs", r#" + fn foo() { } + "#) + .file(".cargo/config", r#" + [build] + rustflags = ["--cfg", "foo"] + "#); + let bar = project("bar") + .file("Cargo.toml", r#" + [package] + name = "bar" + version = "0.0.1" + + [lib] + name = "bar" + "#) + .file("src/lib.rs", r#" + fn bar() { } + #[cfg(not(foo))] + fn bar() { } + "#); + foo.build(); + bar.build(); + + assert_that(foo.cargo("build"), + execs().with_status(0)); +}); + +test!(build_rustflags_normal_source_with_target { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + "#) + .file("src/lib.rs", "") + .file("src/bin/a.rs", "fn main() {}") + .file("examples/b.rs", "fn main() {}") + .file("tests/c.rs", "#[test] fn f() { }") + .file("benches/d.rs", r#" + #![feature(test)] + extern crate test; + #[bench] fn run1(_ben: &mut test::Bencher) { }"#) + .file(".cargo/config", r#" + [build] + rustflags = ["-Z", "bogus"] + "#); + p.build(); + + let ref host = ::rustc_host(); + + // Use RUSTFLAGS to pass an argument that will generate an error + assert_that(p.cargo("build") + .arg("--lib").arg("--target").arg(host), + execs().with_status(101)); + assert_that(p.cargo("build") + .arg("--bin=a").arg("--target").arg(host), + execs().with_status(101)); + assert_that(p.cargo("build") + .arg("--example=b").arg("--target").arg(host), + execs().with_status(101)); + assert_that(p.cargo("test") + .arg("--target").arg(host), + execs().with_status(101)); + assert_that(p.cargo("bench") + .arg("--target").arg(host), + execs().with_status(101)); +}); + +test!(build_rustflags_build_script_with_target { + // RUSTFLAGS should not be passed to rustc for build scripts + // when --target is specified. + // In this test if --cfg foo is passed the build will fail. + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + build = "build.rs" + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + fn main() { } + #[cfg(foo)] + fn main() { } + "#) + .file(".cargo/config", r#" + [build] + rustflags = ["--cfg", "foo"] + "#); + p.build(); + + let host = ::rustc_host(); + assert_that(p.cargo("build") + .arg("--target").arg(host), + execs().with_status(0)); +}); + +test!(build_rustflags_build_script_dep_with_target { + // RUSTFLAGS should not be passed to rustc for build scripts + // when --target is specified. + // In this test if --cfg foo is passed the build will fail. + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + build = "build.rs" + + [build-dependencies.bar] + path = "../bar" + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + fn main() { } + "#) + .file(".cargo/config", r#" + [build] + rustflags = ["--cfg", "foo"] + "#); + let bar = project("bar") + .file("Cargo.toml", r#" + [package] + name = "bar" + version = "0.0.1" + "#) + .file("src/lib.rs", r#" + fn bar() { } + #[cfg(foo)] + fn bar() { } + "#); + foo.build(); + bar.build(); + + let host = ::rustc_host(); + assert_that(foo.cargo("build") + .arg("--target").arg(host), + execs().with_status(0)); +}); + +test!(build_rustflags_plugin_with_target { + // RUSTFLAGS should not be passed to rustc for plugins + // when --target is specified. + // In this test if --cfg foo is passed the build will fail. + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + + [lib] + name = "foo" + plugin = true + "#) + .file("src/lib.rs", r#" + fn main() { } + #[cfg(foo)] + fn main() { } + "#) + .file(".cargo/config", r#" + [build] + rustflags = ["--cfg", "foo"] + "#); + p.build(); + + let host = ::rustc_host(); + assert_that(p.cargo("build") + .arg("--target").arg(host), + execs().with_status(0)); +}); + +test!(build_rustflags_plugin_dep_with_target { + // RUSTFLAGS should not be passed to rustc for plugins + // when --target is specified. + // In this test if --cfg foo is passed the build will fail. + let foo = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + + [lib] + name = "foo" + plugin = true + + [dependencies.bar] + path = "../bar" + "#) + .file("src/lib.rs", r#" + fn foo() { } + "#) + .file(".cargo/config", r#" + [build] + rustflags = ["--cfg", "foo"] + "#); + let bar = project("bar") + .file("Cargo.toml", r#" + [package] + name = "bar" + version = "0.0.1" + + [lib] + name = "bar" + "#) + .file("src/lib.rs", r#" + fn bar() { } + #[cfg(foo)] + fn bar() { } + "#); + foo.build(); + bar.build(); + + let host = ::rustc_host(); + assert_that(foo.cargo("build") + .arg("--target").arg(host), + execs().with_status(0)); +}); + +test!(build_rustflags_recompile { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + "#) + .file("src/lib.rs", ""); + p.build(); + + assert_that(p.cargo("build"), + execs().with_status(0)); + + // Setting RUSTFLAGS forces a recompile + let config = r#" + [build] + rustflags = ["-Z", "bogus"] + "#; + let config_file = paths::root().join("foo/.cargo/config"); + fs::create_dir_all(config_file.parent().unwrap()).unwrap(); + let mut config_file = File::create(config_file).unwrap(); + config_file.write_all(config.as_bytes()).unwrap(); + + assert_that(p.cargo("build"), + execs().with_status(101)); +}); + +test!(build_rustflags_recompile2 { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + "#) + .file("src/lib.rs", ""); + p.build(); + + assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), + execs().with_status(0)); + + // Setting RUSTFLAGS forces a recompile + let config = r#" + [build] + rustflags = ["-Z", "bogus"] + "#; + let config_file = paths::root().join("foo/.cargo/config"); + fs::create_dir_all(config_file.parent().unwrap()).unwrap(); + let mut config_file = File::create(config_file).unwrap(); + config_file.write_all(config.as_bytes()).unwrap(); + + assert_that(p.cargo("build"), + execs().with_status(101)); +}); + +test!(build_rustflags_no_recompile { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + "#) + .file("src/lib.rs", "") + .file(".cargo/config", r#" + [build] + rustflags = ["--cfg", "foo"] + "#); + p.build(); + + assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), + execs().with_status(0)); + assert_that(p.cargo("build").env("RUSTFLAGS", "--cfg foo"), + execs().with_stdout("").with_status(0)); +}); diff --git a/tests/tests.rs b/tests/tests.rs index 6b252a3e2..c163e1aea 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -45,6 +45,7 @@ mod test_cargo_compile_custom_build; mod test_cargo_compile_git_deps; mod test_cargo_compile_path_deps; mod test_cargo_compile_plugins; +mod test_cargo_compile_rustflags; mod test_cargo_cross_compile; mod test_cargo_doc; mod test_cargo_features; -- 2.30.2